package com.tc.cursoflex.controller
{
	import com.tc.cursoflex.event.ShoutsEvent;
	import com.tc.cursoflex.event.SignUpEvent;
	import com.tc.cursoflex.model.ApplicationModel;
	import com.tc.cursoflex.domain.Shouter;
	import com.tc.cursoflex.domain.ShouterTimeline;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import mx.rpc.remoting.RemoteObject;
	
	import org.swizframework.utils.services.ServiceHelper;

	public class SignUpController
	{
		public var ro:RemoteObject = new RemoteObject("signUp");
		
		[Inject]
		public var sh:ServiceHelper;
		
		[Dispatcher]
		public  var dispatcher:IEventDispatcher;
		
		[Bindable]
		[Inject(source="applicationModel")]
		public var model:ApplicationModel;
		
		[EventHandler(event="SignUpEvent.SIGN_UP")]
		public function signUp(event:SignUpEvent):void
		{
			var name:String = event.name;
			var mail:String = event.mail;
			var imageUrl:String = event.imageUrl;
			
			//sh.executeServiceCall(ro.signIn(mail,name,imageUrl), onSignUpResult, onFailResult);
			onSignUpResult({}); //BORRAR!!!
		}
		[EventHandler(event="SignUpEvent.SIGN_IN")]
		public function signIn(event:SignUpEvent):void
		{
			sh.executeServiceCall(ro.signIn(event.name), onSignInResult, onFailResult);
		}
		private function onSignUpResult(o:Object):void
		{
			//model.loggedShouter = o.result;
			var sh:Shouter = new Shouter();
			sh.email = "gastone.frias@gmail.com";
			sh.imageUrl = "http://media.linkedin.com/mpr/pub/image-JqClFGzz-7PDeH7qa-WGi7YQ-toaA4SqIasaKshQH8T0X4e/gaston-frias.jpg";
			sh.name = "Gaston Fritas";
			
			model.loggedShouter = sh;
			
			dispatcher.dispatchEvent(new ShoutsEvent(ShoutsEvent.GET_TIMELINE_FOR,sh));
		}
		private function onSignInResult(o:Object):void
		{
			model.loggedShouter = o.result;
		}
		private function onFailResult(o:Object):void
		{
			trace("Error");
		}
	}
}